package com.ys.zero.biz;

import java.util.List;

import javax.servlet.ServletContext;

import com.ys.zero.bean.User;
import com.ys.zero.dao.UserDao;
import com.ys.zero.util.DBHelper;
import com.ys.zero.util.EmailUtils;
import com.ys.zero.util.VerifyRound;


public class UserBiz {
      private UserDao dao = new UserDao();
      public User login(String account,String  pwd) throws BizException{
    	  if(account ==null||account.trim().isEmpty()) {
    		  throw new BizException("账号不能为空");
    	  }
    	  if(pwd ==null||pwd.trim().isEmpty()) {
    		  throw new BizException("密码不能为空");
    	  }
    	  List<User> list = dao.queryuser(account, pwd);
    	  if(list.isEmpty()) {
    		  throw new BizException("账号或者密码错误");
    	  }else {
    		  return list.get(0);
    	  }
      }
      
      /**
       * 找回密码
       * @param account
       * @return
       * @throws BizException
       */
      public User getBack(String account) throws BizException{
    	  if(account ==null||account.trim().isEmpty()) {
    		  throw new BizException("账号不能为空");
    	  }
    	  try {
        	  User list = dao.getBack(account);
        	  return list;
    	  }catch (Exception e) {
    		  throw new BizException("没有此账号！！");
		}
    	  
      }
      /**
       * 忘记密码发送的验证码
       * @param account
       * @return
       * @throws BizException
       */
      public String sendEmail(String account) throws BizException {
	    	try {
	    		//发送邮件
	        	String email = dao.getEmail(account);
	        	if(email.isEmpty() || email.trim().isEmpty()) {
	        		throw new BizException("你绑定的邮箱为空");
	        	}
	    		EmailUtils eu = new EmailUtils();
	      		VerifyRound v  = new VerifyRound();
	      		String verify = v.getRound();
	      		String Verification ="验证码：" + verify +"    请在15分钟内完成，请勿把验证码给其他人。";
	      		eu.sendMail(email, Verification,"零号网验证码");
	      		return verify;
	    	}catch (Exception e) {
	    		throw new BizException("你绑定的邮箱可能不是本人邮箱");
			}
      }
      
      
      
      
      
      
     /**
      * 注册的biz
      * @param eMail
      * @param password
      * @param code
     * @throws Exception 
      */
	public String insertRegist(String eMail, String password,String name) throws Exception {
		return dao.insertRegist(password,eMail,name);
	}

	public void insertNewPwd(String pwd1, String pwd2,Object account) throws BizException {
		if(pwd1.equals(pwd2)==false) {
			throw new BizException("密码不一致");
		}
		dao.insertNewPwd(pwd2,account);
	}

	/**
	 * 注册发送的验证码
	 * @param mail
	 * @return 
	 * @throws Exception 
	 */
	public String sendEmail1(String mail) throws BizException{
		VerifyRound v  = new VerifyRound();
  		String verify = v.getRound();
  		String Verification ="验证码：" + verify +"    请在15分钟内完成，请勿把验证码给其他人。";
  		boolean isSend = EmailUtils.sendMail(mail, Verification,"零号网验证码");
  		if(isSend==false) {
  			throw new BizException("邮箱不能为空");
  		}
		return verify;
	}  
}
